package com.yejing.redisdemoTest;


import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j

public class Demo1 {


    public Jedis jedis = null;

    @Before
    public void init() {
        if(null == jedis){
            DefaultJedisClientConfig config =
                    DefaultJedisClientConfig.builder() //
                            .password("123456") //
                            .build(); //
            HostAndPort hostPort = new HostAndPort("43.136.67.62", 6379);
            jedis = new Jedis(hostPort, config);
        }
    }

    @Test
    public void test1(){
        String ping = jedis.ping(); // 测试连接
        System.out.println(ping); // 如果成功连接上了Redis服务，此处将会输出PONG
        log.info(ping);
    }

    @Test
    public void test2(){
        // 不存在才设置
        long lock = jedis.setnx("lock", "123456");
        jedis.pexpire("lock",1000*100);
        log.info(lock+""); // 1

    }




    @Test
    public void test3(){
        String key = "A2202020122200001";
        boolean lock = jedis.exists(key);
        log.info(lock+"");

    }


    @Test
    public void testKeys(){

        Set<String> keys = jedis.keys("*");
        keys.stream().forEach(System.out::println);
        keys.stream().forEach(log::info);
    }

    @Test
    public void testRename(){
        String rename = jedis.rename("lst", "lists");
        log.info(rename); // 0k  / redis.clients.jedis.exceptions.JedisDataException: ERR no such key
        testKeys();
    }

    @Test
    public void lock(){
        String key = "A2202020122200001";
        jedis.setnx(key, key + System.currentTimeMillis());
        jedis.pexpire(key,1000*60*60);
    }

    @Test
    public void lockRelease(){
        String key = "A2202020122200001";
        if(jedis.exists(key)){
            long del = jedis.del(key);
            log.info(del+"");
        }
    }

    @Test
    public void testSetex(){
        String key = "A2202020122200001";
        // 原子性操作，先设置值，然后设置过期时间
        String setex = jedis.setex(key, 3600, key);
        log.info(setex);
    }

    @Test
    public void testTtl(){
        String key = "A2202020122200001";
        long ttl = jedis.ttl(key);
        log.info(ttl+"");
    }

    @Test
    public void testPersist(){
        String key = "A2202020122200001";
        // 去掉key的过期时间
        long persist = jedis.persist(key);
        log.info(persist+"");
    }

    @Test
    public void testType(){
        String key = "A2202020122200001";
        String type = jedis.type(key); // string
        log.info(type);
    }

    @Test
    public void testGet(){
        String key = "A2202020122200001";
        // 获取key的值
        String s = jedis.get(key);
        log.info(s);
    }

    @Test
    public void testStrLen(){
        String key = "A2202020122200001";
        String s = jedis.get(key);
        log.info(s);
        long strlen = jedis.strlen(key);
        log.info(strlen+"");
    }

    @Test
    public void testMset(){
        jedis.mset("a","avalue","b","bvalue","c","cvalue");
        Set<String> keys = jedis.keys("*");
//        keys.stream().forEach(e->{
//            String type = jedis.type(e);
//            if(type.equals("string")){
//                String s = jedis.get(e);
//                log.info("{}:{}",e,s);
//            }
//        });
        keys.stream().filter(e->jedis.type(e).equals("string")).forEach(e->{
            log.info("{}:{}",e,jedis.get(e));
        });
    }

    @Test
    public void testMget(){
        Set<String> keys = jedis.keys("*");
        List<String> string = keys.stream().filter(e -> jedis.type(e).equals("string")).collect(Collectors.toList());
        List<String> mget = jedis.mget(string.toArray(new String[]{}));
        mget.stream().forEach(log::info);
    }

    @Test
    public void testGetset(){
        String key = "A2202020122200001";
        String abcde = jedis.getSet(key, "abcde"); // 返回上一次的值，就是旧的值。然后设置新的值进去。
        log.info(abcde);
    }


    @Test
    public void test5(){
        Lock lock = new Lock("A2202020122200001",3600L);
        boolean lock1 = lock.lock();
        if(lock1){
            for (int i = 0; i < 3600; i++) {
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            lock.release();
        }
    }



    @After
    public void end() {
        jedis.close();
    }

}
